﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SBACN.WebSite.Code;
using System.Linq.Expressions;
using SBACN.WebSite.Models;
using Newtonsoft.Json;

namespace SBACN.WebSite.Handler.Admin
{
	/// <summary>
	/// CouncilOpen 的摘要说明
	/// </summary>
	public class CouncilOpen : BaseHandler
	{
		public override void Process()
		{
			HttpContext context = base.httpContext;
			context.Response.ContentType = "text/json";
			var CID = context.Request.QueryString["cid"];

			if (CID == null)
			{
				context.Response.Write("f");
			}
			else
			{
				int cid = Convert.ToInt32(CID);
				Expression<Func<TB_CouncilRelation, bool>> f = (p => p.Index == cid);
				Expression<Func<TB_CouncilRelation, int>> order = (p => p.ID);
				var resultCR = DAL.DALHelper.Instance.GetList<TB_CouncilRelation, int>(f, order, true);


				Expression<Func<TB_Member, bool>> Mf = (p => p.Status);
				Expression<Func<TB_Member, int>> Morder = (p => p.ID);
				var resultM = DAL.DALHelper.Instance.GetList<TB_Member, int>(Mf, Morder, true);

				foreach (var l in resultCR)
				{
					var Mrecord = resultM.Where(p => p.ID == l.MemberID).FirstOrDefault();
					//Mrecord.Type = l.Type;
					DAL.DALHelper.Instance.Update(Mrecord);
				}

				Expression<Func<TB_CouncilInfo, bool>> Cf = (p => p.Status);
				var resultC2 = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_CouncilInfo>(Cf);
				if (resultC2 != null)
				{
					resultC2.Status = false;
					DAL.DALHelper.Instance.Update(resultC2);
				}

				var resultC = DAL.DALHelper.Instance.GetObject<TB_CouncilInfo>("Index", cid);
				resultC.Status = true;
				DAL.DALHelper.Instance.Update(resultC);

				DAL.DALHelper.Instance.SaveChanges();

				context.Response.Write("t");

			}
			context.Response.End();
		}

	}
}